/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework Class Library
 * Version 2.00
 * 
 * Cryptographic Operation Class
 *      Represents and manages specifications for a cryptographic operation that
 * is to be performed.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.IO;

namespace Adaptive.Foundation.Security
{
    /// <summary>
    /// Represents and manages specifications for a cryptographic operation that
    /// is to be performed.
    /// </summary>
    public class CryptographicOperation : DisposableObjectBase
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Algorithm to use in the operation.
        /// </summary>
        private CryptographicAlgorithm _algorithm = CryptographicAlgorithm.None;
        /// <summary>
        /// Type of operation to perform.
        /// </summary>
        private CryptographicOperationType _type = CryptographicOperationType.None;
        /// <summary>
        /// Source data content to be operated upon.
        /// </summary>
        private Stream _sourceData;
        /// <summary>
        /// The result of the last operation.
        /// </summary>
        private CryptographicResult _result;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public CryptographicOperation()
            : base()
        {
            _result = new CryptographicResult();

        }
        /// <summary>
        /// Initializes a new instance of the class to perform the specified operation.
        /// </summary>
        /// <param name="algorithm">
        /// A <see cref="CryptographicAlgorithm"/> enumerated value indicating 
        /// the cryptographic algorithm to be used.
        /// </param>
        /// <param name="operationType">
        /// A <see cref="CryptographicOperationType"/> enumerated value indicating the 
        /// type of operation that is to be performed.
        /// </param>
        public CryptographicOperation(CryptographicAlgorithm algorithm, CryptographicOperationType operationType)
        {
            _result = new CryptographicResult();
            _algorithm = algorithm;
            _type = operationType;
        }
        /// <summary>
        /// Initializes a new instance of the class to perform the specified operation.
        /// </summary>
        /// <param name="algorithm">
        /// A <see cref="CryptographicAlgorithm"/> enumerated value indicating 
        /// the cryptographic algorithm to be used.
        /// </param>
        /// <param name="operationType">
        /// A <see cref="CryptographicOperationType"/> enumerated value indicating the 
        /// type of operation that is to be performed.
        /// </param>
        /// <param name="sourceData">
        /// The data source to be operated upon.
        /// </param>
        public CryptographicOperation(CryptographicAlgorithm algorithm, CryptographicOperationType operationType, 
                                        Stream sourceData)
        {
            _result = new CryptographicResult();
            _algorithm = algorithm;
            _type = operationType;
            _sourceData = sourceData;
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if ((!Disposed) && (disposing))
            {
                if (_result != null)
                    _result.Dispose();
            }

            _result = null;
            _sourceData = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or sets the cryptographic algorithm to use in the operation.
        /// </summary>
        /// <value>
        /// An <see cref="CryptographicAlgorithm"/> enumerated value indicating
        /// which algorithm is used.
        /// </value>
        public CryptographicAlgorithm Algorithm
        {
            get{return _algorithm;}
            set
            {
                _algorithm = value;}
        }
        /// <summary>
        /// Gets or sets the type of operation to perform.
        /// </summary>
        /// <value>
        /// A <see cref="CryptographicOperationType"/> enumerated value indicating the
        /// type of operation to perform.
        /// </value>
        public CryptographicOperationType OperationType 
        {
            get{return _type;}
            set
            {
                _type = value;
            }
        }
        /// <summary>
        /// Gets or sets the reference to the source data content to be operated upon.
        /// </summary>
        /// <value>
        /// A <see cref="Stream"/> instance containing the data upon which a cryptographic
        /// operation is to be performed.
        /// </value>
        public Stream SourceData
        {
            get { return _sourceData; }
            set
            {
                _sourceData = value;
            }
        }
        /// <summary>
        /// Gets the reference to the result of the last operation.
        /// </summary>
        /// <value>
        /// A <see cref="CryptographicResult"/> instance.
        /// </value>
        public CryptographicResult Result
        {
            get { return _result; }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Clears the contents of the object instance without disposing of the instance.
        /// </summary>
        public void Clear()
        {
            _result.Clear();
            _type = CryptographicOperationType.None;
            _algorithm = CryptographicAlgorithm.None;
            _sourceData = null;
        }
        #endregion
      
    }
}
